home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: news.netins.net!isac!gg
- From: gg@isac.hces.com (Greg Goodrich)
- Subject: Re: Q: How to assign structures?
- Message-ID: <1996Feb27.234658.3537@isac.hces.com>
- Organization: Health Care Expert Systems
- X-Newsreader: TIN [version 1.2 PL2]
- References: <4gsalu$dll@ccshst05.cs.uoguelph.ca>
- Date: Tue, 27 Feb 1996 23:46:58 GMT
-
- Toby K Hay (thay@uoguelph.ca) wrote:
- : I need to assign values to structures in one array from structures in
- : another array - in essence I'm copying the whole structure from one array
- : to the other. Can I use simple assignment like this:
-
- : struct StructName {
- : int IVal;
- : float FVal;}
-
- : struct StructName Array1[5], Array2[5];
- : . . .
- : for (i=0;i<5;i++) Array1[i] = Array2[i];
- : . . .
-
- structures in C can be copied by simple assignment as in:
-
- struct1 = struct2;
-
- : And if not that, can I use memcpy() and sizeof() to do the assignment:
-
- : for (i=0;i<5;i++) memcpy(&Array2[i],&Array1[i],sizeof(struct StructName));
- : Or do I have to assign each element of the structure?
-
- You can use memcpy to copy structure data, but as I mentioned above, you
- don't need to. However, make sure that you proceed with caution when
- your structures contain pointers, because you may create an undesired
- side effect by copying a pointer value from one struct to the next.
-
- Greg.
- --
- _______________________________________
- Greg Goodrich - gg@hces.com
- Software Engineer
- PACE Health Management Systems
-